import 'dart:async';
import 'dart:convert';

import 'package:baidu_map_plugin/baidu_map.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';

import 'widgets/native/baidu_map_platform_view.dart';

class BaiduMapviewManager {
  static const MethodChannel _channel = const MethodChannel('baidu_map_plugin');

  static Future<String> get platformVersion async {
    final String version = await _channel.invokeMethod('getPlatformVersion');
    return version;
  }

  static Future<Map> get location async {
    final Map location = await _channel.invokeMethod('getLocation');
    return location;
  }

  static Future<bool> get init async {
    await _channel.invokeMethod('init');
    return true;
  }

  static setMethodCallHandler(Future<dynamic> handler(MethodCall call)) {
    _channel.setMethodCallHandler(handler);
  }
}

class BaiduMapView extends StatefulWidget {
  final String hexColor;
  final AndroidPlatform androidPlatform;

  const BaiduMapView({
    Key key,
    this.hexColor,
    @required this.androidPlatform,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _BaiDuMapViewState();
  }
}

class _BaiDuMapViewState extends State<BaiduMapView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.android ||
        defaultTargetPlatform == TargetPlatform.iOS) {
      return BaiDuMapViewPlatformView(
        creationParams: widget.androidPlatform.flutterNativeBaiDuViewModel,
        platformViewCreatedCallback:
            widget.androidPlatform.androidPlatformViewCreatedCallback,
        selectedOverlay: widget.androidPlatform.onSelectedOverlayOption,
      );
    }
    return Text('activity_indicator插件尚不支持 ');
  }

  void _onPlatformViewCreated(int id) {}
}

typedef AndroidPlatformViewCreatedCallback = Function(
    BaiDuMapViewPlatformViewController controller);

class AndroidPlatform {
  final AndroidPlatformViewCreatedCallback androidPlatformViewCreatedCallback;
  final FlutterNativeBaiDuViewModel flutterNativeBaiDuViewModel;
  final Function(OverlayOption) onSelectedOverlayOption;

  AndroidPlatform({
    this.androidPlatformViewCreatedCallback,
    this.flutterNativeBaiDuViewModel,
    this.onSelectedOverlayOption,
  });
}
